from rgblink_cmd import rgblink_cmd
from layer import *
from x14_setting import *


device_set = x14_setting()

local_buffer = bytes()

def process_cmd(receive_buffer):
	global local_buffer
	local_buffer += receive_buffer
	#print("process_cmd:")
	#print(local_buffer)
	return _process_cmd(local_buffer)
	
	
def _process_cmd(buffer):
	global local_buffer
	if(len(buffer)<19):
		return None
	cmd = rgblink_cmd.fromReplyBytes(buffer)
	if(cmd == None):
		local_buffer = buffer[19:]
		return _process_cmd(local_buffer)
	local_buffer = cmd.block
	if(cmd.cmd == 0xf1):
		block_len = cmd.data[2]
		block_len += cmd.data[3]<<8
		
		if(block_len > 10000):
			block_len = 0
		#print("block_len", block_len)
		if(len(cmd.block)>=block_len):
			local_buffer = cmd.block
			cmd.block = cmd.block[:block_len]
			local_buffer = local_buffer[block_len:]
		else:
			return None
		
	local_buffer = cmd.block
	return cmd
	
interpret_buffer = bytes()

def _interpret_cmd(buffer):
	global interpret_buffer
	if(len(buffer)<19):
		return None
	cmd = rgblink_cmd.fromReplyBytes(buffer)
	if(cmd == None):
		interpret_buffer = buffer[19:]
		return _interpret_cmd(interpret_buffer)
	
	if(cmd.cmd == 0xf1):
		block_len = cmd.data[2]
		block_len += cmd.data[3]<<8
		
		if(block_len > 10000):
			block_len = 0
		
		#print("block_len", block_len)
		if(len(cmd.block)>=block_len):
			interpret_buffer = cmd.block
			cmd.block = cmd.block[:block_len]
			interpret_buffer = interpret_buffer[block_len:]
		else:
			return None
	else:	
		interpret_buffer = cmd.block	
	return cmd
	
def interpret_cmd(receive_buffer):
	global interpret_buffer
	interpret_buffer += receive_buffer
	while(len(interpret_buffer)>=19):
		cmd = _interpret_cmd(interpret_buffer)
		if(cmd == None):
			return
		if(cmd.cmd == 0xF1):
			interpret_0xF1(cmd)
			
		elif(cmd.cmd == 0x68):
			interpret_0x68(cmd)
		elif(cmd.cmd == 0x89):
			device_set.network.IP_adr[0] = cmd.data[0]
			device_set.network.IP_adr[1] = cmd.data[1]
			device_set.network.IP_adr[2] = cmd.data[2]
			device_set.network.IP_adr[3] = cmd.data[3]
		elif(cmd.cmd == 0x85):
			device_set.network.NetMask[0] = cmd.data[0]
			device_set.network.NetMask[1] = cmd.data[1]
			device_set.network.NetMask[2] = cmd.data[2]
			device_set.network.NetMask[3] = cmd.data[3]
		elif(cmd.cmd == 0x8d):
			device_set.network.DefGW[0] = cmd.data[0]
			device_set.network.DefGW[1] = cmd.data[1]
			device_set.network.DefGW[2] = cmd.data[2]
			device_set.network.DefGW[3] = cmd.data[3]
		elif(cmd.cmd == 0x8e):
			if(cmd.data[0] == 0x03):
				device_set.network.DHCP = cmd.data[1]
			elif(cmd.data[0] == 0x05):
				device_set.network.MAC_adr[0] = cmd.data[1]
				device_set.network.MAC_adr[1] = cmd.data[2]
				device_set.network.MAC_adr[2] = cmd.data[3]
			elif(cmd.data[0] == 0x07):
				device_set.network.MAC_adr[3] = cmd.data[1]
				device_set.network.MAC_adr[4] = cmd.data[2]
				device_set.network.MAC_adr[5] = cmd.data[3]

def interpret_0xF1(cmd):
	#print("interpret_0xF1:", cmd)
	if(cmd.data[0] == 0x00):
		device_set.previewView.layer[cmd.data[1]].fromBytes(cmd.block)
		#print("layer", cmd.data[1], layer1)
		
	elif(cmd.data[0] == 0x05):
		output_len = int((len(cmd.block)-1)/8)
		for i in range(0, output_len):
			rect1 = rect.fromBytes(cmd.block[i*8:])
			device_set.outputArea[i] = rect1
	elif(cmd.data[0] == 0x09):
		device_set.inputSet[cmd.data[1]].fromBytes(cmd.block)
		#print("inputSet", cmd.data[1], inputSet1)
	elif(cmd.data[0] == 0x0a):
		input = cmd.data[1]*4
		for i in range(0, 4):
			device_set.inputSource[input+i].type = cmd.block[0+i]
			device_set.inputSource[input+i].format = cmd.block[4+i]
			device_set.inputSource[input+i].width = cmd.block[8+i*2]+(cmd.block[8+i*2+1]<<8)
			device_set.inputSource[input+i].height = cmd.block[16+i*2]+(cmd.block[16+i*2+1]<<8)
			device_set.inputSource[input+i].vfreq = cmd.block[24+i]
def interpret_0x68(cmd):
	#print("interpret_0x68", cmd);
	if(cmd.data[0] == 0x01):
		device_type = (cmd.data[1]<<16)+(cmd.data[2]<<8)+cmd.data[3]
		if(device_set.device_type != device_type):
			device_set.setDeviceType(device_type)
			print("set device type=", hex(device_type))
	